#include <iostream>
#include <mutex>
#include <thread>
// #include <mutex>

using namespace std;
class Widget
{
public:
    Widget()
    {
        cout << "Widget 无参构造 = default" << endl;
    };
    ~Widget()
    {
        cout << "析构函数 = default" << endl;
    };

    void fun()
    {
        std::lock_guard<std::mutex> lock(lock_);
        for (size_t i = 0; i < 10; i++)
        {
            this_thread::sleep_for(chrono::microseconds(1000000));
            // this_thread::sleep_for(chrono::seconds(1));

            // this_thread::sleep_for(chrono::milliseconds(1000));

            cout << i << ".Widget::fun run id:" << this_thread::get_id() << endl;
        }
    }
    void func2(){
        std::lock_guard<std::mutex> lock(lock_);

        for (size_t i = 0; i < 5; i++)
        {
            this_thread::sleep_for(chrono::seconds(1));
            
            cout<<"func2:getid =  "<<this_thread::get_id()<<endl;
            
        }
        
    }

private:
    std::mutex lock_;
};

void TestThread1(Widget *w)
{
    w->fun();
    w->func2();
}

int main()
{
    Widget *w = new Widget();
    std::thread t1(&TestThread1, w);
    std::thread t2(&TestThread1, w);

    t1.join();
    t2.join();

    return 0;
}
